package Pages.sub;

import javax.microedition.lcdui.Graphics;
import Const.GConfig;
import Pages.Game;
import Pages.CPlayer;

public class Move {
	private int iX; // toa do x
	private int iY; // toa do y
	private int iXEnd; // toa do x cuoi
	private int iYEnd; // toa do y cuoi
	private int iW; // chieu rong
	private int iH; // chieu cao
	private double iDegree; // goc
	private int iStep; // van toc di chuyen
	private boolean isMove; // di chuyen
	private int indexImg; // vi tri anh ve
	private int iSee; // huong nhin
	private int iNumCollison; // va cham
	private int iNumLive; // so mang
	private boolean hasBox; // mang theo kho bao

	public Move(int ix, int iy, int indexImg) {
		iX = ix;
		iY = iy;
		iXEnd = iX;
		iYEnd = iY;
		this.indexImg = indexImg;
		iW = CPlayer.imgMove[this.indexImg].getWidth();
		iH = CPlayer.imgMove[this.indexImg].getHeight();
		iDegree = 0;
		iStep = 20;
		isMove = false;
		iSee = GConfig.k_iRight;
		iNumCollison = 0;
		iNumLive = GConfig.k_iNumLive;
		hasBox = false;
	}

	private int getIndexImageMan(){
		return (iSee == GConfig.k_iLeft ? 5 : iSee == GConfig.k_iDown ? 8 : iSee == GConfig.k_iRight ? 11 : 14);
	}
	
	public void paint(Graphics g) {
		g.drawImage(CPlayer.imgMove[indexImg], iX + CPlayer.s_iMapX, iY + CPlayer.s_iMapY, 0);
		if (hasBox)
			g.drawImage(CPlayer.imgBoxSmall, iX + CPlayer.s_iMapX, iY + iH / 2
					+ CPlayer.s_iMapY, Graphics.HCENTER | Graphics.VCENTER);
	}

	public void update() {
		if (Game.timeTick % 15 == 0) {// hieu ung anh sang
			updateStart();
			updateBox();
		}
		if (indexImg >= 5)
			updateMen();
	}

	public void setPosition(int x, int y) {
		// System.out.println("---------setPosition");
		int dx, dy;
		double sin, iOEnd;
		dx = Math.abs(x - iX);
		dy = Math.abs(y - iY);
		iOEnd = Math.sqrt(dx * dx + dy * dy);

		// 0 -> 45
		if (x >= iX && y >= iY && dx > dy) {
			sin = dy / iOEnd;
			iDegree = Math.toDegrees(sin);
			// System.out.println("0 -> 45");
			// System.out.println(" " + iDegree);
			iDegree = iDegree + 0;
			iSee = GConfig.k_iRight;
		}
		// 45 -> 90
		else if (x > iX && y >= iY && dx <= dy) {
			sin = dx / iOEnd;
			iDegree = Math.toDegrees(sin);
			// System.out.println("45 -> 90");
			// System.out.println(" " + iDegree);
			iDegree = (45 - iDegree) + 45;
			iSee = GConfig.k_iDown;
		}
		// 90 -> 135
		else if (x <= iX && y >= iY && dy > dx) {
			sin = dx / iOEnd;
			iDegree = Math.toDegrees(sin);
			// System.out.println("90 -> 135");
			// System.out.println(" " + iDegree);
			iDegree = iDegree + 90;
			iSee = GConfig.k_iDown;
		}
		// 135 - 180
		else if (x <= iX && y > iY && dy <= dx) {
			sin = dy / iOEnd;
			iDegree = Math.toDegrees(sin);
			// System.out.println("135 - 180");
			// System.out.println(" " + iDegree);
			iDegree = (45 - iDegree) + 135;
			iSee = GConfig.k_iLeft;
		}
		// 180 - 225
		else if (x <= iX && y <= iY && dx > dy) {
			sin = dy / iOEnd;
			iDegree = Math.toDegrees(sin);
			// System.out.println("180 - 225");
			// System.out.println(" " + iDegree);
			iDegree = iDegree + 180;
			iSee = GConfig.k_iLeft;
		}
		// 225 - 270
		else if (x <= iX && y < iY && dx <= dy) {
			sin = dx / iOEnd;
			iDegree = Math.toDegrees(sin);
			// System.out.println("225 - 270");
			// System.out.println(" " + iDegree);
			iDegree = (45 - iDegree) + 225;
			iSee = GConfig.k_iUp;
		}
		// 270 - 315
		else if (x >= iX && y <= iY && dx < dy) {
			sin = dx / iOEnd;
			iDegree = Math.toDegrees(sin);
			// System.out.println("270 - 315");
			// System.out.println(" " + iDegree);
			iDegree = iDegree + 270;
			iSee = GConfig.k_iUp;
		}
		// 315 - 360
		else if (x >= iX && y < iY && dx >= dy) {
			sin = dy / iOEnd;
			iDegree = Math.toDegrees(sin);
			// System.out.println("315 - 360");
			// System.out.println(" " + iDegree);
			iDegree = (45 - iDegree) + 315;
			iSee = GConfig.k_iRight;
		}

		if (iOEnd > 0) {
			isMove = true;
			iXEnd = x;
			iYEnd = y;
			iNumCollison = 0;
			// System.out.println(iXEnd + ", " + iYEnd);
		}
	}

	public void move() {
		double rads;
		float cos, sin;
		int x, y;

		rads = Math.toRadians(iDegree);
		cos = (float) Math.cos(rads);
		sin = (float) Math.sin(rads);
		x = (int) (iX + iStep * cos);
		y = (int) (iY + iStep * sin);

		if (0 <= iDegree && iDegree < 90) {// xuong phai
			if (x > iXEnd) {
				x = iXEnd;
				if (y != iYEnd) {
					iDegree = 90;
					iSee = GConfig.k_iDown;
				}
			}
			if (y > iYEnd) {
				y = iYEnd;
				if (x != iXEnd) {
					iDegree = 0;
					iSee = GConfig.k_iRight;
				}
			}
			if (x >= iXEnd && y >= iYEnd)
				isMove = false;
		} else if (90 <= iDegree && iDegree < 180) {// xuong trai
			if (x < iXEnd) {
				x = iXEnd;
				if (y != iYEnd) {
					iDegree = 90;
					iSee = GConfig.k_iDown;
				}
			}
			if (y > iYEnd) {
				y = iYEnd;
				if (x != iXEnd) {
					iDegree = 180;
					iSee = GConfig.k_iLeft;
				}
			}
			if (x <= iXEnd && y >= iYEnd)
				isMove = false;
		} else if (180 <= iDegree && iDegree < 270) {// len trai
			if (x < iXEnd) {
				x = iXEnd;
				if (y != iYEnd) {
					iDegree = 270;
					iSee = GConfig.k_iUp;
				}
			}
			if (y < iYEnd) {
				y = iYEnd;
				if (x != iXEnd) {
					iDegree = 180;
					iSee = GConfig.k_iLeft;
				}
			}
			if (x <= iXEnd && y <= iYEnd)
				isMove = false;
		} else if (270 <= iDegree && iDegree < 360) {// len phai
			if (x > iXEnd) {
				x = iXEnd;
				if (y != iYEnd) {
					iDegree = 270;
					iSee = GConfig.k_iUp;
				}
			}
			if (y < iYEnd) {
				y = iYEnd;
				if (x != iXEnd) {
					iDegree = 0;
					iSee = GConfig.k_iRight;
				}
			}
			if (x >= iXEnd && y <= iYEnd)
				isMove = false;
		}

		// if (checkCollision(iXEnd, iYEnd, iStep, iStep) == false) {
		if (isMove) {
			iX = x;
			iY = y;
		} else {
			// System.out.println("--------move: Da di den!");
			iX = iXEnd;
			iY = iYEnd;
		}
	}

	public void moveLeft() {
		iSee = GConfig.k_iLeft;
		int x = iX - iStep;
		if (x >= 0)
			iX = x;
		else
			iX = 0;//GConfig.k_iCol * GConfig.k_iCellWidth - iW;
	}

	public void moveRight() {
		iSee = GConfig.k_iRight;
		int x = iX + iStep;
		if (x <= (GConfig.k_iCol * GConfig.k_iCellWidth - iW))
			iX = x;
		else
			iX = (GConfig.k_iCol * GConfig.k_iCellWidth - iW);//0;
	}

	public void moveUp() {
		iSee = GConfig.k_iUp;
		int y = iY - iStep;
		if (y >= 0)
			iY = y;
		else
			iY = 0;//GConfig.k_iRow * GConfig.k_iCellHigh - iH;
	}

	public void moveDown() {
		iSee = GConfig.k_iDown;
		int y = iY + iStep;
		if (y <= (GConfig.k_iRow * GConfig.k_iCellHigh - iH))
			iY = y;
		else
			iY = (GConfig.k_iRow * GConfig.k_iCellHigh - iH);//0;
	}

	public void updateStart() {
		if (indexImg == 2)
			indexImg = 3;
		else if (indexImg == 3)
			indexImg = 2;
	}

	public void updateBox() {
		if (indexImg == 1)
			indexImg = 4;
		else if (indexImg == 4)
			indexImg = 1;
	}

	public void updateMen() {
		if (isMove) {
			if (Game.timeTick % 2 == 0)
				if ((indexImg - 5) % 3 == 0)
					indexImg = getIndexImageMan();
				else
					indexImg++;
		} else if (iNumCollison > 0) {
			// hieu ung choang
			if (Game.timeTick % 2 == 0) {
				iNumCollison--;
				if (iSee == GConfig.k_iRight)
					iSee = GConfig.k_iDown;
				else if (iSee == GConfig.k_iDown)
					iSee = GConfig.k_iLeft;
				else if (iSee == GConfig.k_iLeft)
					iSee = GConfig.k_iUp;
				else
					iSee = GConfig.k_iRight;
				indexImg = getIndexImageMan();
			}
		}
	}

	public int getX() {
		return iX;
	}

	public int getY() {
		return iY;
	}

	public void setX(int ix) {
		iX = ix;
	}

	public void setY(int iy) {
		iY = iy;
	}

	public int getXEnd() {
		return iXEnd;
	}

	public int getYEnd() {
		return iYEnd;
	}

	public int getW() {
		return iW;
	}

	public int getH() {
		return iH;
	}

	public double getDegree() {
		return iDegree;
	}

	public void setStep(int step) {
		if (step > 0) {
			iStep = step;
		}
	}

	public int getSee() {
		return iSee;
	}

	public int getNumLive() {
		return iNumLive;
	}

	public void setNumLive(int numLive) {
		iNumLive = numLive;
	}

	public int getNumCollison() {
		return iNumCollison;
	}

	public void setNumCollison(int numCollison) {
		iNumCollison = numCollison;
	}

	public void setMove(boolean isMove) {
		this.isMove = isMove;
	}

	public boolean isMove() {
		return isMove;
	}

	public int getStep() {
		return iStep;
	}

	public void setHasBox(boolean hasBox) {
		this.hasBox = hasBox;
	}

	public boolean isHasBox() {
		return hasBox;
	}
}
